package com.yimihaodi.sliptimeview;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 *
 * 滑动操作时间demo
 * @author 冷锋
 *
 */
public class SlipTimeView extends HorizontalScrollView {

    private LinearLayout ll;

    public SlipTimeView(Context context) {
        super(context);
        initSlipTimeView();
    }

    public SlipTimeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSlipTimeView();
    }

    public SlipTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSlipTimeView();
    }


    private void initSlipTimeView() {

        //HorizontalScrollView  root view
        ll = new LinearLayout(getContext());
        ll.setOrientation(LinearLayout.HORIZONTAL);

        for (int i=0 ;i<20;i++){
            //时间标签view
            TextView tv = new TextView(getContext());
            tv.setText("TimeView= "+i);
            tv.setTextColor(Color.WHITE);
            tv.setMinimumWidth(200);
            tv.setHeight(50);

            //操作时间底部view可以将此控件换成Layout
            //再添加view 添加的view划过的child view 重新设置颜色就可以了
            TextView tv3 = new TextView(getContext());
            tv3.setMinimumWidth(200);
            tv3.setHeight(250);
            tv3.setBackgroundColor(getResources().getColor(R.color.colorAccent));


            //childview  item
            LinearLayout ll1 = new LinearLayout(getContext());
            ll1.setOrientation(LinearLayout.VERTICAL);
            ll1.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

            ll1.addView(tv);
            ll1.addView(tv3);

            //组装成item
            ll.addView(ll1);

            Log.i("SlipTimeView","TimeView = "+i);
        }
        //添加到HorizontalScrollView 一定是单个view才可以
        addView(ll);


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                Log.i("MotionEvent","MotionEvent.ACTION_DOWN");
                break;
            case MotionEvent.ACTION_UP:
                Log.i("MotionEvent","MotionEvent.ACTION_UP");
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                Log.i("MotionEvent","MotionEvent.ACTION_POINTER_DOWN");
                break;
            case MotionEvent.ACTION_POINTER_UP:
                Log.i("MotionEvent","MotionEvent.ACTION_POINTER_UP");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.i("MotionEvent","MotionEvent.ACTION_MOVE");

                //操作时间和HorizontalScrollView的root view 滚动
                scrollTo(X ,Y);
                break;
        }
        return super.onTouchEvent(event);
    }
}
